package com.tc.zigbee.zcl;

import com.tc.zigbee.zcl.command.AbstractZclCommand;
import com.tc.zigbee.zcl.command.AddGroupResponse;
import com.tc.zigbee.zcl.command.AddSceneResponse;
import com.tc.zigbee.zcl.command.ConfigReportResponse;
import com.tc.zigbee.zcl.command.GroupMemberResponse;
import com.tc.zigbee.zcl.command.ReadAttributeResponse;
import com.tc.zigbee.zcl.command.RemoveAllSceneResponse;
import com.tc.zigbee.zcl.command.RemoveGroupResponse;
import com.tc.zigbee.zcl.command.RemoveSceneResponse;
import com.tc.zigbee.zcl.command.Report;
import com.tc.zigbee.zcl.command.SceneMemberResponse;
import com.tc.zigbee.zcl.command.StoreSceneResponse;
import com.tc.zigbee.zcl.command.ViewGroupResponse;
import com.tc.zigbee.zcl.command.ViewSceneResponse;

public class ZclMessageParser {
	static public AbstractZclCommand parse(short clusterId,
			byte fc, byte commandId, byte[] data) {

			byte csb = (byte)(0x03 & fc);
		boolean cs = (csb == 1);
		if (!cs) {

				if (commandId == ZclConstants.ZCL_CMD_READ_RSP) {
					return new ReadAttributeResponse(data);
				}
				if (commandId == ZclConstants.ZCL_CMD_CONFIG_REPORT_RSP) {
					return new ConfigReportResponse(data);
				}
				if (commandId == ZclConstants.ZCL_CMD_REPORT) {
					return new Report(data);
				}
				if (commandId == ZclConstants.ZCL_CMD_DEFAULT_RSP) {
					System.out.println("defaultResponse");
				}
		}else{
			if (clusterId == ZclConstants.ZCL_CLUSTER_ID_GEN_GROUPS) {
				byte dir = (byte)(0x08 & fc);
				if (commandId == 0x00 && (dir == 0x08)) {
					return new AddGroupResponse(data);
				}
				if (commandId == 0x01 && (dir == 0x08)) {
					return new ViewGroupResponse(data);
				}
				if (commandId == 0x02 && (dir == 0x08)) {
					return new GroupMemberResponse(data);
				}
				if (commandId == 0x03 && (dir == 0x08)) {
					return new RemoveGroupResponse(data);
				}
				
			}
			if (clusterId == ZclConstants.ZCL_CLUSTER_ID_GEN_SCENES) {
				byte dir = (byte)(0x08 & fc);
				if (commandId == 0x00 && (dir == 0x08)) {
					return new AddSceneResponse(data);
				}
				if (commandId == 0x01 && (dir == 0x08)) {
					return new ViewSceneResponse(data);
				}
				if (commandId == 0x02 && (dir == 0x08)) {
					return new RemoveSceneResponse(data);
				}
				if (commandId == 0x03 && (dir == 0x08)) {
					return new RemoveAllSceneResponse(data);
				}
				
				if (commandId == 0x04 && (dir == 0x08)) {
					return new StoreSceneResponse(data);
				}

				if (commandId == 0x06 && (dir == 0x08)) {
					return new SceneMemberResponse(data);
				}
			}
		}
		return null;
	}

}
